package br.upe.dsc.caeto.utils.xml;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;

import br.upe.dsc.caeto.core.application.Application;
import br.upe.dsc.caeto.core.cacheunit.CacheUnit;
import br.upe.dsc.caeto.core.cacheunit.CacheUnitExplorer;
import br.upe.dsc.caeto.core.cacheunit.CacheUnitSimple;
import br.upe.dsc.caeto.core.project.Project;

public class PropertiesXml {
	private String userDir;
	private String separator;
	private String propertiesFile;
	
	public PropertiesXml() {
        userDir = System.getProperties().getProperty("user.dir");
        separator = System.getProperties().getProperty("file.separator");
        propertiesFile = userDir + separator + "properties.xml";
	}
	
	public void write(Document document) {
		File file = new File(propertiesFile);
		try {
			FileWriter writer = new FileWriter(file);
			XMLOutputter outputter = new XMLOutputter(org.jdom.output.Format.getPrettyFormat());
			outputter.output(document, writer);
		} catch (java.io.IOException e) {
			System.out.println("ERROR:");
			System.out.print("PropertiesXml class, ");
			System.out.println("error in write method -> IOException.");
			//e.printStackTrace();
		}		
	}
	
	public void write(Project project) {
		File file = new File(propertiesFile);
		Document document = null;
		if (file.exists()) {
			try {
				document = read();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			document.getRootElement().addContent(createChildElement(project));
		}
		else {
			Element rootElement = new Element("caeto");
			rootElement.addContent(createChildElement(project));
			document = new Document(rootElement);
		}
		try {
			FileWriter writer = new FileWriter(file, false);
			XMLOutputter outputter = new XMLOutputter(org.jdom.output.Format.getPrettyFormat());
			outputter.output(document, writer);
		} catch (java.io.IOException e) {
			System.out.println("ERROR:");
			System.out.print("PropertiesXml class, ");
			System.out.println("error in write method -> IOException.");
			//e.printStackTrace();
		}
	}
	
	public Document read() throws java.io.FileNotFoundException,
								  java.io.IOException {
		Document document = null;
		File file = new File(propertiesFile);
		
		if (file.exists()) {
			SAXBuilder builder = new SAXBuilder();	
			try {
				document = builder.build(file);
			}
			catch (org.jdom.JDOMException e) {
				e.printStackTrace();
			}
		}
		return document;
	}
	
	public Element createChildElement(Project project) {
		Element projectElement = new Element("project");
		Element pathElement = new Element("path");

		projectElement.setAttribute("name", project.getName());
		pathElement.addContent(project.getPath());
		projectElement.addContent(pathElement);
		
		return projectElement;
	}
}
